import java.awt.Dimension;
import java.awt.Image;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Observable;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class GameMenu extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public GameMenuStartGameButton start_button_observable;
	public GameMenuSetUserNameFrame set_username_frame;

	private BufferedImage game_menu_image;
	private MyListener game_menu_listener;

	private JButton game_menu_start_button;
	private JLabel game_menu_image_label;
	
	private Menu game_menu_1, game_menu_2;
	private MenuBar game_menu_bar;
	private MenuItem game_menu_item_1, game_menu_item_2, game_menu_item_3;

	public GameMenu(GameMenuSetUserNameFrame game_menu_set_username_frame) {
		this.start_button_observable = new GameMenuStartGameButton();
		this.set_username_frame = game_menu_set_username_frame;
		this.game_menu_listener = new MyListener(this.start_button_observable,
				this.set_username_frame);

		this.game_menu_image = null;
		try {
			this.game_menu_image = ImageIO.read(new File(
					"C:/planes_multiplayer.jpg"));
		} catch (IOException e) {
			e.printStackTrace();
		}

		game_menu_bar = new MenuBar();
		game_menu_1 = new Menu("Game");
		game_menu_2 = new Menu("Settings");

		this.game_menu_item_1 = new MenuItem("Start Game");
		this.game_menu_item_2 = new MenuItem("Exit Game");
		this.game_menu_item_3 = new MenuItem("Set Username");

		game_menu_start_button = new JButton("Start Game");
		game_menu_start_button.setBounds(new Rectangle(20, 20, 160, 160));
		game_menu_start_button.setLocation(new Point(300, 300));
		game_menu_start_button.addActionListener(this);

		this.game_menu_image_label = new JLabel(new ImageIcon(
				".\\planes_multiplayer.jpg"));
		this.game_menu_image_label.setSize(new Dimension(900, 900));

		this.game_menu_image_label = new JLabel(new ImageIcon(
				this.game_menu_image.getScaledInstance(
						this.game_menu_image_label.getWidth(),
						this.game_menu_image_label.getHeight(),
						Image.SCALE_SMOOTH)));

		this.add(this.game_menu_image_label);

		this.game_menu_item_1.addActionListener(this.game_menu_listener);
		this.game_menu_item_2.addActionListener(this.game_menu_listener);
		this.game_menu_item_3.addActionListener(this.game_menu_listener);

		this.game_menu_bar.add(game_menu_1);
		this.game_menu_bar.add(game_menu_2);
		this.game_menu_1.add(this.game_menu_item_1);
		this.game_menu_1.add(this.game_menu_item_2);
		this.game_menu_2.add(this.game_menu_item_3);
		this.setMenuBar(this.game_menu_bar);
	}

	@Override
	public void actionPerformed(ActionEvent act) {
		if (act.getSource() == this.game_menu_start_button) {
			System.out.println("Start Button Pressed");
			this.start_button_observable.start_button_pressed();
		}

	}

	class MyListener implements ActionListener {

		private GameMenuStartGameButton start_button_observable;
		private GameMenuSetUserNameFrame set_user_name_frame;

		public MyListener(GameMenuStartGameButton start_button_observable,
				GameMenuSetUserNameFrame set_user_name_frame) {
			this.start_button_observable = start_button_observable;
			this.set_user_name_frame = set_user_name_frame;
		}

		public void actionPerformed(ActionEvent event) {

			String str = ((MenuItem) event.getSource()).getLabel();
			if (str.endsWith("Start Game"))
				this.start_button_observable.start_button_pressed();
			if (str.endsWith("Exit Game"))
				System.exit(0);
			if (str.equals("Set Username")) {
				this.set_user_name_frame.game_menu_set_username_frame
						.setVisible(true);
			}
		}
	}

	class GameMenuStartGameButton extends Observable {
		public void start_button_pressed() {
			this.setChanged();
			this.notifyObservers();
		}

	}

}
